/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.models.Si_quocgia;
import com.hvtt.web.models.Si_tinh;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class SI_TinhController implements Serializable{
     private WEBConnection DAL=new WEBConnection();
    private Si_tinh item =new Si_tinh();
    
    public Si_tinh getItem()
    {
        return item;
    }
    public void setItem(Si_tinh item)
    {
        this.item=item;
    }
    
   
    public List<Si_tinh> findAllTinhByQuocGia() throws SQLException
    {
        WEBParamCollection Params=WEBParamCollection.getInstance();
        String strQuocGia=String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuocGiaID"));
        Params.add("".equals(strQuocGia)?"VN":strQuocGia);
        List<Si_tinh> lst= DAL.executeQuery("Select* from SI_Tinh where QuocGiaID=? order by SI_Tinh.ThuTu asc",Command.sqlText,Params,Si_tinh.class);
        return lst;        
    }
    
    public List<Si_tinh> findAllTinh() throws SQLException
    {
        if(Utilities.getRequest().getParameter("type")!=null 
                && Utilities.getRequest().getParameter("value")!=null && Utilities.getRequest().getParameter("QG")!=null)
        {
            this.item.setQuocGiaID(String.valueOf(Utilities.getRequest().getParameter("QG")));
        }
        
        WEBParamCollection Params=new WEBParamCollection();
        Params.add("".equals(this.getItem().getQuocGiaID().trim())?"VN":this.getItem().getQuocGiaID());
        
        List<Si_tinh> lst= DAL.executeQuery("Select* from SI_Tinh where QuocGiaID=? order by SI_Tinh.ThuTu asc",Command.sqlText,Params,Si_tinh.class);
        return lst;        
    }
    public List<Si_quocgia> findAllQuocGia() throws SQLException
    {
        List<Si_quocgia> lst= DAL.executeQuery("Select* from SI_QuocGia", Si_quocgia.class);
        if("".equals(this.getItem().getQuocGiaID().trim()) && lst.size()>0)
            this.getItem().setQuocGiaID(lst.get(0).getQuocGiaID());
        return lst;        
    }
    
     private String validateFields() throws SQLException {
        String str="";
        if (StringUtils.isBlank(this.getItem().getTinhID()))
        {
            str= "  + " + Utilities.getLanguages("TinhID")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if (StringUtils.isBlank(this.getItem().getTenTinh()))
        {
            str+="  + " + Utilities.getLanguages("TenTinh")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
       
        if("".equals(str.trim()))
            return "";
        return "<b style='color:black;'>" + Utilities.getLanguages("AdMsg0010") + ":</b> <br/>" +  "<b style='color:red;'>" + str + "</b>";
    }
    
    
    
    public void btnSave_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setTinhID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TinhID")));
          this.getItem().setTenTinh(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TenTinh")));
          this.getItem().setQuocGiaID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuocGiaID")));
          
          this.getItem().setTrangThai(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TrangThai").trim()));
          this.getItem().setThuTu(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ThuTu").trim()));
          
           String result = this.validateFields();
      
            if (!result.isEmpty()) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status",result);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                RequestContext.getCurrentInstance().execute("update=\"growl\"");
                return;
          }
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Save");
            Params.add(this.item.getTinhID());
            Params.add(this.item.getTenTinh());
            Params.add(this.item.getTrangThai());
            Params.add(this.item.getQuocGiaID());
            Params.add(this.item.getThuTu());
            //Params.add(this.item.getBieuTuong());
            DAL.executeUpdate("{call Proc_SI_Tinh_In_Up_Del(?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/si/provices.html");
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
     public void btnDelete_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setTinhID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TinhID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Delete");
            Params.add(this.item.getTinhID());
            Params.add("");
            Params.add(0);
            Params.add("");
             Params.add(0);
            DAL.executeUpdate("{call Proc_SI_Tinh_In_Up_Del(?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/si/provices.html");
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    public void btnUpdate_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setTinhID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TinhID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("GetByKey");
            Params.add(this.item.getTinhID());
            Params.add("");
            Params.add(0);
            Params.add("");
            Params.add(0);
            List<Si_tinh> lst= DAL.executeQuery("{call Proc_SI_Tinh_In_Up_Del(?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params,Si_tinh.class);
            if(lst!=null && lst.size()>0)
            {
                this.setItem(lst.get(0));
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/si/provices/" + this.getItem().getQuocGiaID() + "/new.html");
            }
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
    public void cboQuocGiaID_Change(ActionEvent event) throws IOException, SQLException {
          this.getItem().setQuocGiaID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuocGiaID")));
    }
    public void cboQuocGiaID_Change1(ActionEvent event) throws IOException, SQLException {
          this.getItem().setQuocGiaID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuocGiaID")));
    }
    
}
